<template>
	<!-- 没有二维码时测试用 -->
	<div class="home">
<!--		<div class="attende attende_scan" @click="openScanCode" v-if="key == ''||key == false">请扫二维码</div>-->
		<div class="attende attende_input" v-if="key==''||key==false">
			<label>请输入口令：</label><input maxlength="10" type="text" v-model="meeting_password"/>
			<div class="attende_scan" @click="mettingpwdDiscern">
				确认
			</div>
		</div>
		<div class="attende" v-if="key != ''">
			<div class="canhui" @click="navigateToAttende">
				<img src="http://file.rzkeji.com/web/dang/canhuiren.png" />
				<span>参会人</span>
			</div>
			<div class="zhuchi" @click="navigateToHost" >
				<img src="http://file.rzkeji.com/web/dang/zhuchiren.png" />
				<span>主持人</span>
			</div>
		</div>
		<!--<div class="host" @click="navigateToHost" v-if="key != ''">主持人</div>-->
		
		
		<!-- 登录 -->
		<u-popup @touchmove.native.stop v-model="show" @close="handleClose"
		             border-radius="12" mode="center" width="80%">
		      <view class="popup-container">
		        <view class="shopping-name">人工智能党建</view>
		        <div @click="bindGetUserInfo" style='border: 1px solid #dcdcdc;width: 50%;margin: 0 auto;border-radius: 25rpx;overflow: hidden;'>
		          <!-- <div class="login-mode">
		           <image class="mode-icon" mode="widthFix" src="https://rzkeji.oss-cn-shenzhen.aliyuncs.com/web/dang/default-photo.png" />
		            <image class="mode-icon" mode="widthFix" src="../../static/img/logo.png" />
		          </div> -->
		          <div class="login-mode" style='background-color:#e21918 ;color: #fff;padding-bottom: 25rpx;'>
		            <text class="mode-name">点击授权登录</text>
		          </div>
		        </div>
		        <div class="login-mode">
		          <view class="desc">
		            <text>人工智能党建助手手机端介绍：</text>
		            <text>1、参与内部会议</text>
		            <text>2、手机同步PPT内容</text>
		            <text>3、参与会议讨论</text>
		          </view>
		        </div>
		
		
		<!--        <view class="login-desc">-->
		<!--          <uni-data-checkbox multiple selectedTextColor="#666" v-if="initial"-->
		<!--                             selectedColor="#ef7b56" @change="protocolChange"-->
		<!--                             :localdata="[{value: 0, text: '我已阅读并同意'}]" />-->
		<!--          <text class="prefix-text" v-else>我已阅读并同意</text>-->
		<!--          <text class="user-agreement" @click="toAgreement">《用户服务协议》</text>-->
		<!--        </view>-->
		        <view class="refuse-login" @click="handleClose">稍后再说</view>
		      </view>
		    </u-popup>
	</div>
</template>

<script>
	import ajax from "../../api/ajax";
	import {postCode} from "../../api";
	export default {
		data(){
			return{
				key:"",
                type:"",
				old_key:'',
				meeting:'',
				meeting_device:'',
				meeting_join:'',
				meeting_password:'',
				 show:false,
			}
		},
		onLoad(query){
			const scene = decodeURIComponent(query.scene)

      var key  = "";
      var type = "";
      if(!scene){
        key = query.key
        type = query.type
      }else {
        key  = this.getQueryVariable('key',scene)
        type = this.getQueryVariable('type',scene)
      }

			if(key){
				this.key = key
			}

			if(type){
			  this.type = type
				uni.setStorageSync('type',type);
			}

			if(uni.getStorageSync('key') != '' && uni.getStorageSync('key') != key){
				this.old_key = uni.getStorageSync('key')
			}


		},
		onShow(){
		  this.getUserInfo();
      if(this.type != '' && this.type == 'auth'){
          this.checkDeviceLogin();
          // uni.removeStorageSync('type');
      }else{
        this.getMeetingInfo();
      }

		},
		onReady(){

		},
		methods:{
      async getUserInfo(){
        let res=await this.$api.getSimpleUserInfo({
          user_token:uni.getStorageSync('userToken')
        });
        if(res.data.level == "success"){
          console.log("success");
        }else {
          if(res.data.data == 'no_login'){
            uni.setStorageSync('history_url','/pages/home/home?key='+this.key+'&type='+this.type);
            // uni.redirectTo({
            //   url:"/pages/index/index"
            // })
			this.show=true
			console.log('请登录');
          }else {
            uni.showToast({
              icon:'none',
              title:res.data.message
            })
          }

        }
      },
      async checkDeviceLogin(){
          var _this = this
          let params = {
            auth_key:this.key,
            user_token: uni.getStorageSync('userToken'),
          }
        let res = await _this.$api.smallAppLoginCheck(params);
        if(res.data.level == "success"){
          uni.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      },
			getQueryVariable(variable,scene){
				var vars = scene.split("&");
				for (var i=0;i<vars.length;i++) {
					var pair = vars[i].split("=");
					if(pair[0] == variable){return pair[1];}
				}
				return(false);
			},
			async getMeetingInfo(){
				let params = {
					key:this.old_key,
				}
				var _this = this;
				let res = await _this.$api.getMeetingInfo(params);
				if(res.data.level == "success"){
					var resultData      = res.data.data;
					_this.meeting        = resultData.meeting
					_this.meeting_join   = resultData.meeting_join
					_this.meeting_device = resultData.meeting_device
					console.log(resultData)
					if (resultData.meeting_active != null && resultData.meeting_active.end_time == null && _this.meeting_join != null) {
						wx.showModal({
							title: '继续参加会议吗？',
							content: '您参与的【'+_this.meeting.title+'】会议还在进行',
							success(res) {
								if (res.confirm) {
									uni.setStorageSync('key',_this.old_key);
									if (_this.meeting_join.type == null || _this.meeting_join.type == 'attendee'){
										uni.navigateTo({
											url:"/pages/setRealname/setRealname"
										})
									}else {
										uni.navigateTo({
											url:"/pages/setInfo/setInfo"
										})
									}
								} else{
									uni.setStorageSync('key',_this.key);
									_this.old_key = '';
								}
							}
						})
					}else {
						uni.setStorageSync('key',_this.key);
					}
				}else {
					uni.setStorageSync('key',_this.key);
				}

			},
			//识别口令
			async mettingpwdDiscern(){
				let params = {
					join_number:this.meeting_password
				}
				const res = await this.$api.getKeyByNumber(params);
				console.log(res)
				if (res.data.level == 'success'){
					this.key = res.data.data.key;
					this.getMeetingInfo()
				} else {
					uni.showToast({
						title: res.data.message,
						icon: 'none',
						duration: 1000
					})
				}
			},
			//获取URL参数
			getUrlArg(){
				// const scene = decodeURIComponent(query.scene)
				// var pages = getCurrentPages();
				// console.log(pages)
				// var arg = pages[pages.length - 1].options;
				// if(arg.hasOwnProperty('key')){
				// 	this.key = arg.key;
				// 	uni.setStorageSync('key',arg.key);
				// }
			},
			openScanCode(){
				wx.scanCode({
					success: (res) => {
						if(res.errMsg == "scanCode:ok"){
							console.log(res)
							// uni.navigateTo({
							// 	url:`/pages/answer/answer?key=666`
							// })
							// if(res.result){
							// 	//跳转答题
							// 	let key=res.result.split('?key=')[1];
							// 	getApp().globalData.meetingKey=key;
							// 	uni.navigateTo({
							// 		url:`/pages/answer/answer`
							// 	})
							// 	return
							// }
							// //跳转会议或答题
							uni.navigateTo({
								url:"/"+res.path
							})
						}
					}
				})
			},
			navigateToAttende(){
				uni.navigateTo({
					url:"/pages/setRealname/setRealname"
				})

			},
			navigateToHost(){
				uni.navigateTo({
					url:"/pages/setInfo/setInfo"
				})
			},
			handleClose(){
			  this.show = false
			},
			
			bindGetUserInfo(e){
			  uni.showLoading()
			  var This = this;
			  wx.getUserProfile({
			    lang : 'zh_CN',
			    desc: '获取您的昵称和头像信息',
			    success : res2 => {
			      console.log(res2)
			      This.$store.dispatch('_setWXUserInfo',res2); //将用户信息保存的VUEX
			      wx.login({
			        success: res => {
			          const code = res.code;
			          console.log(code)
			          let parmas = {
			            raw_data:res2.rawData,
			            signature:res2.signature,
			            code:code
			          };
			          //发送code到服务器获取数据
			          postCode(parmas).then(respones => {
			            console.log(respones)
			            if(respones.data.level=="success"){
			              uni.setStorageSync('userToken',respones.data.data);
			              // uni.navigateBack({
			              //   delta:1
			              // })
			              // uni.redirectTo({
			              //   url:"/pages/index/index"
			              // })
			              This.show = false
			              uni.reLaunch({ url:"/pages/home/home"})
			              wx.showToast({
			                title: '登录成功',
			                icon: 'success',
			                duration: 2000
			              })
			            }else{
			              uni.hideLoading()
			              uni.showToast(respones.data.message,'none',1000)
			            }
			          })
			        },
			      });
			    }
			  })
			
			},
		}
	}
</script>

<style lang="scss" scoped>
	.home{
		width: 100vw;
		height: 100vh;
		/*background-color: #d90913;*/
		/*background: url("http://file.rzkeji.com/web/dang/wxapp/back.png") no-repeat;*/
		background-color: #da2424;
		background-size: 100%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		.attende{
			width: 100%;
			text-align: center;
			color: #FFFFFF;
			margin: 1rem 0;
			.zhuchi,.canhui{
				display: inline-block;
				margin: 0 0.5rem;
				width: 5rem;
				height: 5rem;
				img{
					width: 100%;
					height: 100%;
				}
				span{
					display: inline-block;
					margin-top: 0.3rem;
					color: #ffff00;
					font-size: 0.8rem;
				}
			}
		}
		.attende_input{
			width: 80%;
			label,input{
				display: inline-block;
				vertical-align: middle;
			}
			label{
				width: 40%;
			}
			input{
				width: 50%;
				padding: 10rpx 5%;
				border-bottom: 2rpx solid #ffffff;
				/*border-radius: 0.5rem;*/
			}
		}
		.attende_scan{
			width: 50%;
			height: 2rem;
			line-height: 2rem;
			background-color: #f0860b;
			border-radius: 0.5rem;
			margin: 50rpx auto;
		}
		/*.host{*/
		/*width: 80%;*/

		/*height: 80rpx;*/
		/*line-height: 80rpx;*/
		/*text-align: center;*/
		/*color: #FFFFFF;*/
		/*background-color: #ffda2f;*/
		/*border-radius: 12rpx;*/
		/*}*/
		/*.attende{*/
		/*margin-bottom: 40rpx;*/
		/*}*/
	}
	
	.popup-container {
	  width: 100%;
	  padding: 50rpx 50rpx 30rpx;
	  background-color: #fff;
	
	  .shopping-name {
	    margin-bottom: 30rpx;
	    color: #303133;
	    font-size: 36rpx;
	    font-weight: bold;
	    text-align: center;
	  }
	
	  .login-mode {
	    position: relative;
	    display: flex;
	    justify-content: center;
	    align-content: center;
	    gap: 20rpx;
	    padding-top: 25rpx;
	    background-color: #fff;
	    font-size: 28rpx;
	    color: #606366;
	    transition: opacity .15s;
	    &:active {
	      opacity: .7;
	    }
	    .mode-icon {
	      display: block;
	      width: 100rpx;
	      min-width: 100rpx;
	      height: 100rpx;
	    }
	    .mask-wrapper {
	      position: absolute;
	      top: 0;
	      left: 0;
	      width: 100%;
	      height: 100%;
	    }
	
	    .desc{
	      color: #333333;
	      display: flex;
	      flex-direction: column;
	
	      text{
	        padding-left: 10px;
	        padding-top: 5px;
	        font-size: 0.8rem;
	      }
	    }
	  }
	
	  .login-desc {
	    display: flex;
	    justify-content: center;
	    align-items: center;
	    width: 100%;
	    margin-top: 60rpx;
	    text-align: center;
	    font-size: 24rpx;
	
	    .prefix-text {
	      margin-left: 45rpx;
	      color: #666;
	      font-size: 28rpx;
	      line-height: 28rpx;
	      white-space: nowrap;
	    }
	
	    .user-agreement {
	      white-space: nowrap;
	      color: #ef7b56;
	    }
	  }
	
	  .refuse-login {
	    margin-top: 40rpx;
	    font-size: 28rpx;
	    text-align: center;
	    color: #999;
		border: 1px solid #999;
		padding: 20rpx;
		border-radius: 25rpx;
	  }
	}
</style>
